package com.wxlgroup.springboot.service.serviceImp;
import cn.hutool.core.bean.BeanUtil;
import com.wxlgroup.springboot.commons.Response;
import com.wxlgroup.springboot.dao.LoginDao;
import com.wxlgroup.springboot.entity.User;
import com.wxlgroup.springboot.entity.UserDto;
import com.wxlgroup.springboot.service.LoginService;
import com.wxlgroup.springboot.utils.TokenUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @auther:wxl
 * @Date:2022/5/23-05-23-15:47
 * @Description:com.wxlgroup.springboot.service.serviceImp
 * @Version:1.0
 */
@Service
public class LoginServiceImp implements LoginService {

    @Resource
    private LoginDao loginDao;

    @Override
    public Response login(UserDto userDto) {
        User user = loginDao.login(userDto);
        if(user!=null){
            //将查询到的user对象信息属性复制给useerDto对象
            BeanUtil.copyProperties(user,userDto,true);
            //根据用户名和密码生成token
            String token = TokenUtils.genToken(user.getId().toString(), user.getPassword());
            userDto.setToken(token);
            return new Response("200",userDto,"登陆成功");
        }else {
            return new Response("500",null,"账号或密码错误！");
        }

    }
}
